import 'package:flutter/material.dart';

// 定义 Toast 类型的枚举
enum ToastType { success, error, warn }

// 显示自定义提示框的函数
void showCustomToast(BuildContext context, String message, ToastType type) {
  Color backgroundColor;
  String iconPath;

  // 根据类型设置背景颜色和图标路径
  switch (type) {
    case ToastType.success:
      backgroundColor = Colors.white;
      iconPath = 'assets/success.png'; // 替换为成功图标路径
      break;
    case ToastType.error:
      backgroundColor = Colors.white;
      iconPath = 'assets/error.png'; // 替换为错误图标路径
      break;
    case ToastType.warn:
      backgroundColor = Colors.white;
      iconPath = 'assets/warn.png'; // 替换为警告图标路径
      break;
  }

  OverlayEntry overlayEntry;

  overlayEntry = OverlayEntry(
    builder: (context) => Positioned(
      top: 20,
      left: 0,
      right: 0,
      child: Material(
        color: Colors.transparent,
        child: Container(
          alignment: Alignment.center,
          child: Container(
            padding: EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: backgroundColor,
              borderRadius: BorderRadius.circular(8),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Image.asset(
                  iconPath,
                  width: 24, // 图标宽度
                  height: 24, // 图标高度
                ),
                SizedBox(width: 8), // 图标和文本之间的间距
                Text(
                  message,
                  style: TextStyle(color: Colors.black),
                ),
              ],
            ),
          ),
        ),
      ),
    ),
  );

  Overlay.of(context).insert(overlayEntry);

  // 设置显示时长
  Future.delayed(Duration(seconds: 3), () {
    overlayEntry.remove();
  });
}
